

using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Services;
using JTC.MiniCommerce.Store.Services.PaymentService;
using JTC.MiniCommerce.Store.Web.Controls;
using JTC.MiniCommerce.Web.admin.controls.configuration.paymentproviders;

namespace JTC.MiniCommerce.Web.admin.controls.configuration {
  public partial class providerconfiguration : AdminControl {

    #region Constants

    private const string PROVIDER_ID = "ProviderId";
    private const string NAME = "Name";

    #endregion

    #region Member Variables

    private ProviderType _providerType;
    private string defaultProviderName = string.Empty;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        if(!Page.IsPostBack) {
          SetProviderConfiguration();
        }
        SetConfigurationControl();
        SetProviderConfigurationProperties();
      }
      catch(Exception ex) {
        Logger.Error(typeof(providerconfiguration).Name + ".Page_Load", ex);
        MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSetProvider control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSetProvider_Click(object sender, EventArgs e) {
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the provider configuration.
    /// </summary>
    private void SetProviderConfiguration() {
      ProviderCollection providerCollection =
        new ProviderController().FetchByProviderType(this.ProviderType);
      if(providerCollection.Count > 0) {
        ddlProviders.DataSource = providerCollection;
        ddlProviders.DataValueField = PROVIDER_ID;
        ddlProviders.DataTextField = NAME;
        ddlProviders.DataBind();
        //if there is a defaultProvider, then set it as the selected one.
        if(!string.IsNullOrEmpty(defaultProviderName)) {
          ListItem listItem = ddlProviders.Items.FindByText(defaultProviderName);
          if(listItem != null) {
            listItem.Selected = true;
          }
        }
      }
      else {
        pnlInstalledProviders.Visible = false;
        ddlProviders.Visible = false;
        btnSetProvider.Visible = false;
        MasterPage.MessageCenter.DisplayFailureMessage(SetNoProvidersInstalledMessage());
      }
    }

    /// <summary>
    /// Sets the no providers installed message.
    /// </summary>
    /// <returns></returns>
    private string SetNoProvidersInstalledMessage() {
      string message = string.Empty;
      switch(this.ProviderType) {
        case ProviderType.PaymentProvider:
          message = LocalizationUtility.GetText("lblNoPaymentProvidersInstalled");
          break;
        case ProviderType.ShippingProvider:
          message = LocalizationUtility.GetText("lblNoShippingProvidersInstalled");
          break;
        case ProviderType.TaxProvider:
          message = LocalizationUtility.GetText("lblNoTaxProvidersInstalled");
          break;
      }
      return message;
    }

    /// <summary>
    /// Sets the configuration control.
    /// </summary>
    private void SetConfigurationControl() {
      switch(this.ProviderType) {
        case ProviderType.PaymentProvider:
          LoadPaymentProviderConfigurationControl();
          break;
        case ProviderType.ShippingProvider:
          LoadShippingProviderConfigurationControl();
          break;
        case ProviderType.TaxProvider:
          LoadTaxProviderConfigurationControl();
          break;
      }
    }

    /// <summary>
    /// Sets the provider configuration properties.
    /// </summary>
    private void SetProviderConfigurationProperties() {
      btnSetProvider.Text = LocalizationUtility.GetText("btnSetProvider");

      switch(this.ProviderType) {
        case ProviderType.PaymentProvider:
          lblConfiguration.Text = LocalizationUtility.GetText("lblPaymentConfiguration");
          lblInstalledProviders.Text = LocalizationUtility.GetText("lblInstalledPaymentProviders");
          pnlInstalledProviders.GroupingText = LocalizationUtility.GetText("pnlPaymentProviders");
          break;
        case ProviderType.ShippingProvider:
          lblConfiguration.Text = LocalizationUtility.GetText("lblShippingConfiguration");
          lblInstalledProviders.Text = LocalizationUtility.GetText("lblInstalledShippingProviders");
          pnlInstalledProviders.GroupingText = LocalizationUtility.GetText("pnlShippingProviders");
          break;
        case ProviderType.TaxProvider:
          lblConfiguration.Text = LocalizationUtility.GetText("lblTaxConfiguration");
          lblInstalledProviders.Text = LocalizationUtility.GetText("lblInstalledTaxProviders");
          pnlInstalledProviders.GroupingText = LocalizationUtility.GetText("pnlTaxProviders");
          break;
      }
    }

    /// <summary>
    /// Loads the payment provider configuration control.
    /// </summary>
    private void LoadPaymentProviderConfigurationControl() {
      if(ddlProviders.Items.Count > 0) {
        ProviderCollection providerCollection =
          new ProviderController().FetchByID(ddlProviders.SelectedValue);
        PaymentConfigurationControl paymentConfigurationControl =
          Page.LoadControl(providerCollection[0].ConfigurationControlPath) as PaymentConfigurationControl;
        paymentConfigurationControl.Provider = providerCollection[0];
        pnlConfiguration.Controls.Clear();
        pnlConfiguration.Controls.Add(paymentConfigurationControl);
      }
    }

    /// <summary>
    /// Loads the shipping provider configuration control.
    /// </summary>
    private void LoadShippingProviderConfigurationControl() {
      if(ddlProviders.Items.Count > 0) {
        ProviderCollection providerCollection = new ProviderController().FetchByID(ddlProviders.SelectedValue);
        ShippingConfigurationControl shippingConfigurationControl =
          Page.LoadControl(providerCollection[0].ConfigurationControlPath) as ShippingConfigurationControl;
        shippingConfigurationControl.Provider = providerCollection[0];
        pnlConfiguration.Controls.Clear();
        pnlConfiguration.Controls.Add(shippingConfigurationControl);
      }
    }

    /// <summary>
    /// Loads the tax provider configuration control.
    /// </summary>
    private void LoadTaxProviderConfigurationControl() {
      if(ddlProviders.Items.Count > 0) {
        ProviderCollection providerCollection = new ProviderController().FetchByID(ddlProviders.SelectedValue);
        TaxConfigurationControl taxConfigurationControl = Page.LoadControl(providerCollection[0].ConfigurationControlPath) as TaxConfigurationControl;
        taxConfigurationControl.Provider = providerCollection[0];
        pnlConfiguration.Controls.Clear();
        pnlConfiguration.Controls.Add(taxConfigurationControl);
      }
    }

    #endregion

    #endregion

    #region Properties

    public ProviderType ProviderType {
      get {
        return _providerType;
      }
      set {
        _providerType = value;
      }
    }

    #endregion

  }
}